package org.elasticsearch.plugin.helloworld;

import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.RestRequest.Method;
import org.elasticsearch.rest.RestResponse;


public class HelloWorldHandler extends BaseRestHandler {

    @Inject
    protected HelloWorldHandler(Settings settings, RestController controller, Client client) {
		super(settings, controller, client);
		controller.registerHandler(Method.GET, "/hello/", this);
		controller.registerHandler(Method.GET, "/hello/{name}", this);
	}


	


	@Override
	protected void handleRequest(RestRequest request, RestChannel channel, Client client) throws Exception {
		logger.debug("HelloWorldAction.handleRequest called");
		final String name = request.hasParam("name") ? request.param("name") : "world";
		
		String content = "{\"success\":true, \"message\":\"hello " +name+ "\"}";
		
		RestResponse response = new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, content);
		channel.sendResponse(response);
	}
}
